#include <gccore.h>
#include <wiiuse/wpad.h>
#include "config.h"
#include "keys.h"
#include "syslog.h"

static bool useDefaultIntro = true;
static bool sequenceStarted = false;
static s8 keySequence = 0;
static s8 prevKeySequence;
static s8 candidates[16];
static s8 currentKey= _NOKEY;
#ifdef DEBUG
	syslog_instance_t *syslog;
	
	void keysSetDebugInstance(syslog_instance_t * syslogInstance) 
	{
		syslog = syslogInstance;
	}	
#endif



bool setConfig(bool defaultIntro) 
{
	useDefaultIntro = defaultIntro;
	
	if (!defaultIntro) 
	{
		//Candidates for hidden shortcuts
		s8 hiddenShortcutCount;
		hiddenShortcutCount = GetHiddenShortcutCount();

		#ifdef DEBUG 				
			Syslog_SendFormat(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: Hidden shortcut count is %d", hiddenShortcutCount);		
		#endif	
		//Fill the candidates with the key counts of the shortcuts
		s8 i;
		HiddenShortcut * hiddenShortcut;
		for (i=0;i<hiddenShortcutCount;i++) {
			hiddenShortcut = GetHiddenShortcut(i);
			candidates[i] = hiddenShortcut->keyCount;
			#ifdef DEBUG 				
				Syslog_SendFormat(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: Candidate %d->%d", i, candidates[i]);		
			#endif			
		}
	}

}


u8 respondToKey(s32 pressed, s32 held, s8 * hiddenShortcutIndex) 
{
	prevKeySequence = keySequence;

	if((pressed & WPAD_BUTTON_A))
	{
		#ifdef DEBUG 
			Syslog_Send(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: pressed A");		
		#endif
	
		if (!sequenceStarted) {
			if (IsKeyAssigned(_CFG_A) || useDefaultIntro) 
			{
				return _CFG_A;
			}
		} else {
			currentKey = _CFG_A;keySequence++;
		}
	} 

	if((pressed & WPAD_BUTTON_B) )
	{
		#ifdef DEBUG 
			Syslog_Send(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: pressed B");		
		#endif
	
		if (!sequenceStarted) {
			if (IsKeyAssigned(_CFG_B) || useDefaultIntro) 
			{
				return _CFG_B;
			}
		} else {
			currentKey = _CFG_B;keySequence++;
		}

	}

	if (!useDefaultIntro) 
	{
		if((pressed & WPAD_BUTTON_HOME) )
		{
		#ifdef DEBUG 
			Syslog_Send(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: pressed HOME");		
		#endif
		
			#ifdef DEBUG 
				Syslog_Send(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: sequence started");		
			#endif
		
			//Hidden shortcuts not available in default intro... so...
			if (!useDefaultIntro) 
			{
				sequenceStarted = true;
			}
		} 	

		if((pressed & WPAD_BUTTON_LEFT) )
		{	
		#ifdef DEBUG 
			Syslog_Send(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: pressed LEFT");		
		#endif
			if (!sequenceStarted) {
				if (IsKeyAssigned(_CFG_LEFT)) 
				{
					return _CFG_LEFT;
				}
			} else {
				currentKey = _CFG_LEFT;keySequence++;
			}

		}

		if((pressed & WPAD_BUTTON_RIGHT) )
		{
		#ifdef DEBUG 
			Syslog_Send(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: pressed RIGHT");		
		#endif
		
			if (!sequenceStarted) {
				if (IsKeyAssigned(_CFG_RIGHT)) 
				{
					return _CFG_RIGHT;
				}
			} else {
				currentKey = _CFG_RIGHT;keySequence++;
			}

		}

		if((pressed & WPAD_BUTTON_UP) )
		{
			if (!sequenceStarted) {
				if (IsKeyAssigned(_CFG_UP)) 
				{
					return _CFG_UP;
				}
			} else {
				currentKey = _CFG_UP;keySequence++;
			}
		}

		if((pressed & WPAD_BUTTON_DOWN) )
		{
			if (!sequenceStarted) {
				if (IsKeyAssigned(_CFG_DOWN)) 
				{
					return _CFG_DOWN;
				}
			} else {
				currentKey = _CFG_DOWN;keySequence++;
			}				
		}
		
		if((pressed & WPAD_BUTTON_1) )
		{
			if (!sequenceStarted) {
				if (IsKeyAssigned(_CFG_1)) 
				{
					return _CFG_1;
				}
			} else {
				currentKey = _CFG_1;keySequence++;
			}
		}

		if((pressed & WPAD_BUTTON_2) )
		{
			if (!sequenceStarted) {
				if (IsKeyAssigned(_CFG_2)) 
				{
					return _CFG_2;
				}
			} else {
				currentKey = _CFG_2;keySequence++;
			}
		}

		if ((pressed & WPAD_BUTTON_PLUS) )
		{
			if (!sequenceStarted) {
				if (IsKeyAssigned(_CFG_PLUS)) 
				{
					return _CFG_PLUS;
				}
			} else {
				currentKey = _CFG_PLUS;keySequence++;
			}
		}
		
		if ((pressed & WPAD_BUTTON_MINUS) )
		{
			if (!sequenceStarted) {
				if (IsKeyAssigned(_CFG_MINUS)) 
				{
					return _CFG_MINUS;
				}
			} else {
				currentKey = _CFG_MINUS;keySequence++;
			}
		}

		//Check if state changed
		
		if ((sequenceStarted) && (keySequence!=prevKeySequence)) 
		{		
			#ifdef DEBUG 				
				Syslog_SendFormat(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: Check Candidates with %d, %d", currentKey, keySequence);		
			#endif
		
			*hiddenShortcutIndex = CheckAndUpdateCandidates(candidates, currentKey, keySequence);
			
			if (*hiddenShortcutIndex == CANDIDATE_STOP) {
				#ifdef DEBUG 
					Syslog_Send(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: sequence stop");		
				#endif

				sequenceStarted = false;
				keySequence = 0;
				prevKeySequence = 0;
			} else if (*hiddenShortcutIndex > CANDIDATE_CONTINUE) {
				#ifdef DEBUG 
					Syslog_Send(syslog, SYSLOG_PRI_INTERNAL, SYSLOG_SEV_DEBUG, "syslog: sequence complete");		
				#endif

				return _CFG_COMPOSITE;
			}
		} 
		
	}
	
	return _NOKEY;
}
